package yanyi.community.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yanyi.community.dto.QuestionDTO;
import yanyi.community.mapper.QuestionMapper;
import yanyi.community.mapper.UserMapper;
import yanyi.community.model.Question;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class QuestionService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuestionMapper questionMapper;

    public PageInfo findAll(Map<String,Object> map,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuestionDTO> dtoList = new ArrayList<>();
        List<Question> questionList = questionMapper.findAll(map);
        PageInfo pi = new PageInfo(questionList);
        QuestionDTO dto = null;
        for (Question question : questionList) {
            dto = new QuestionDTO();
            BeanUtils.copyProperties(question, dto);
            dto.setUser(userMapper.selectByPrimaryKey(question.getCreator()));
            dtoList.add(dto);
        }
        PageInfo pageInfo = new PageInfo();
        BeanUtils.copyProperties(pi, pageInfo);
        pageInfo.setList(dtoList);
        return pageInfo;
    }

    public QuestionDTO getById(Long id) {
        QuestionDTO dto = new QuestionDTO();
        Question question = questionMapper.selectByPrimaryKey(id);
        if (question != null) {
            BeanUtils.copyProperties(question, dto);
            dto.setUser(userMapper.selectByPrimaryKey(question.getCreator()));
        }
        return dto;
    }

    public void createOrUpdate(Question question) {
        if (question.getId() == null) {
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(question.getGmtCreate());
            question.setCommentCount(0);
            question.setViewCount(0);
            question.setLikeCount(0);
            questionMapper.insert(question);
        } else {
            question.setGmtModified(System.currentTimeMillis());
            questionMapper.updateByPrimaryKeySelective(question);
        }
    }

    /**
     * 增加阅读次数
     */
    public void updateViewCount(Long id) {
        Question question = new Question();
        question.setId(id);
        question.setViewCount(1);
        questionMapper.updateViewCount(question);
    }

    public List<QuestionDTO> selectRelated(QuestionDTO questionDTO) {
        if (StringUtils.isBlank(questionDTO.getTag())) {
            return new ArrayList<>();
        }
        String[] tags = StringUtils.split(questionDTO.getTag(), ",");
        String regexTag = Arrays.stream(tags).collect(Collectors.joining("|"));
        Question question = new Question();
        question.setId(questionDTO.getId());
        question.setTag(regexTag);
        List<Question> questions = questionMapper.selectRelated(question);
        List<QuestionDTO> questionDTOS = questions.stream().map(q -> {
            QuestionDTO dto = new QuestionDTO();
            BeanUtils.copyProperties(q, dto);
            return dto;
        }).collect(Collectors.toList());
        return questionDTOS;
    }
}
